package at.jku.ssw.fortress.ui.outline;

import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;

import at.jku.ssw.fortress.ui.Editor;
import at.jku.ssw.fortress.ui.ModelListener;

public class FortressContentOutlinePage extends ContentOutlinePage implements ModelListener {

	private Editor editor;
	
	public FortressContentOutlinePage(Editor editor) {
		this.editor = editor;
		editor.addModelListener(this);
	}
	
	public void createControl(Composite parent) {
		super.createControl(parent);
		TreeViewer viewer = getTreeViewer();
		viewer.setContentProvider(new OutlineContentProvider());
		viewer.setLabelProvider(new OutlineLabelProvider());
		viewer.addSelectionChangedListener(this);
		viewer.setInput(editor.getAbstractModel());
	}
	
	public void dispose() {
		super.dispose();
		editor = null;
	}
	
	public void modelChanged() {
		getTreeViewer().setInput(editor.getAbstractModel());
	}

	public void select(Definition def) {
		if(def != null) {
			getTreeViewer().setSelection(new StructuredSelection(def));
		}
	}

}
